package com.talent.logincontroller;

import com.talent.entities.dto.Admin;
import com.talent.entities.dto.User;
import com.talent.entities.vo.LoginUser;
import com.talent.loginservice.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 周意朗
 * Date: 2022/3/20
 * @since JDK 11
 */
@RestController
@EnableAutoConfiguration
@Slf4j
@CrossOrigin
public class LoginController {
    @Value("${server.port}")
    private String serverPort;

    @Resource
    private LoginService service;

    private HttpServletRequest request;

    @GetMapping(value = "/logintest/{id}")
    public String getReady(@PathVariable("id") Long id,
                           HttpServletRequest request,
                           Model model,
                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return "我是 nacos 注册,port:" + serverPort + "\t id:" + id;
    }


    @GetMapping("/provider/loginforuser/{tel}/{pwd}")
    public User loginMethod(@PathVariable("tel") String tel,
                            @PathVariable("pwd") String pwd,
                            HttpServletRequest request,
                            Model model,
                            HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        if (tel == null || tel.length() != 11 || pwd == null || pwd.length() < 6) {
            return null;
        }
        LoginUser user = new LoginUser(tel, pwd);
        return service.loginMethod(user);
    }

    @GetMapping("/provider/loginofadmin/{tel}/{pwd}")
    public Admin loginMethodAdmin(@PathVariable("tel") String tel,
                                  @PathVariable("pwd") String pwd,
                                  HttpServletRequest request,
                                  Model model,
                                  HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        if (tel == null || tel.length() != 11 || pwd == null || pwd.length() < 6) {
            return null;
        }
        Admin admin = new Admin();
        admin.setAdminPhone(tel);
        admin.setAdminPwd(pwd);
        return service.loginMethodAdmin(admin);
    }

    @GetMapping("/provider/userudt/{tel}/{email}/{areaid}")
    public User updateUser(@PathVariable("tel") String tel,
                           @PathVariable("email") String email,
                           @PathVariable("areaid") String areaid,
                           HttpServletRequest request,
                           Model model,
                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        User user = new User();
        user.setUserTel(tel);
        user.setUserEmail(email);
        user.setUserAreaId(Integer.parseInt(areaid));
        Boolean flag = service.updateUser(user);
        if (flag) {
            return service.afterUpdateUser(tel);
        }
        return null;
    }

    @PostMapping("/provider/userpwdchange")
    public Boolean updateUserPassword(@RequestBody User userupdate,
                                      HttpServletRequest request,
                                      Model model,
                                      HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        return service.updateUserPassword(userupdate.getUserTel(), userupdate.getUserPwd(), userupdate.getUserId());
    }

    @PostMapping("/provider/registuser")
    public Boolean registUser(@RequestBody User user,
                              HttpServletRequest request,
                              Model model,
                              HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        if (user == null) {
            new Exception("userNull").printStackTrace();
        }
        return service.registUser(user);
    }
}
